package uiComponents;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
/**
 * Creates a JPane which resizes dynamically when the scroll bars are used.
 */
public class InfScrollPane extends JScrollPane implements AdjustmentListener{
	JPanel p = new JPanel();
	/**
	 * Creates a giant JPanel with permanent scroll panes.
	 */
	public InfScrollPane(){
		add(p);
		p.setBackground(Color.white);
		p.setPreferredSize(new Dimension(1000,1000));
		this.setViewportView(p);
		p.setLayout(null);
		setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
		setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
		p.setVisible(true);
		this.getHorizontalScrollBar().addAdjustmentListener(this);
		this.getVerticalScrollBar().addAdjustmentListener(this);
		setAutoscrolls(true);
	}
	/**
	 * Listens for the scrollbar to move. When it approaches the max value, 
	 * increase the max value to give the impression of increased size.
	 */
	//TODO: Make this part work.
	public void adjustmentValueChanged(AdjustmentEvent arg0) {
		JScrollBar h = this.getHorizontalScrollBar();
		JScrollBar v = this.getVerticalScrollBar();
		if (arg0.getAdjustable().equals(h)){
			if (h.getValue() > h.getMaximum()-20){
				p.setPreferredSize(new Dimension(p.getWidth()+10, p.getHeight()));
				System.out.println("H");
			}
		}
		else if(arg0.getAdjustable().equals(v)){
			if (v.getValue() > v.getMaximum()-10){
				p.setPreferredSize(new Dimension(p.getWidth(), p.getHeight()+10));
				System.out.println("V");
			}
		}
	}
	/**
	 * Returns the JPane which actually holds all the content shown on the screen.
	 * @return The content pane of the window.
	 */
	public JPanel getContentPane(){
		return p;
	}
	
}
